# This file is part of Sonedyan.
#
# Sonedyan is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation;
# either version 3 of the License, or (at your option) any
# later version.
#
# Sonedyan is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public.
# If not, see <http://www.gnu.org/licenses/>.
#
# Copyright (C) 2009-2012 Jimmy Dubuisson <jimmy.dubuisson@gmail.com>

# convert Eckmann dataset to graphml

linksFile = open("words_all_links.txt", "r")
graphmlFile = open("eckmann-dataset.xml", "w")

line = linksFile.readline().strip()
vertices = set() 
edges = {}
counter = 0

# load links
while line:
	key, value = line.split();
	edges[counter] = line
	vertices.add(key)
	vertices.add(value)
	line = linksFile.readline().strip()
	counter = counter + 1

linksFile.close()

graphmlFile.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
graphmlFile.write("<graphml xmlns=\"http://graphml.graphdrawing.org/xmlns/graphml\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"  xsi:schemaLocation=\"http://graphml.graphdrawing.org/xmlns/graphml\">\n")
graphmlFile.write("<graph edgedefault=\"directed\">\n")

for i in vertices:
	graphmlFile.write("<node id=\"" + i + "\" />\n")

for k in edges.iterkeys():
	key, value = edges[k].split();
	graphmlFile.write("<edge id=\"" + key + "|" + value + "\" directed=\"true\" source=\"" + key + "\" target=\"" + value + "\"/>\n")

graphmlFile.write("</graph>\n")
graphmlFile.write("</graphml>\n")

graphmlFile.close()


